#include "stdafx.h"
#include "WindowLogAppender.h"

#include <stdlib.h>

CWindowLogAppender::CWindowLogAppender(HWND hListCtrl)
	: m_pLogList(nullptr)
	, m_hListCtrl(hListCtrl)
{
}


CWindowLogAppender::~CWindowLogAppender()
{
}

bool CWindowLogAppender::initAppender()
{
	m_pLogList = new CListCtrl();
	m_pLogList->Attach(m_hListCtrl);

	m_pLogList->InsertColumn(0, L"Level", 0, 50);
	m_pLogList->InsertColumn(1, L"Time", 0, 180);
	m_pLogList->InsertColumn(2, L"ThreadID", 0, 70);
	m_pLogList->InsertColumn(3, L"Content", 0);

	return true;
}

bool CWindowLogAppender::uninitAppender()
{
	m_pLogList->Detach();
	return true;
}

void CWindowLogAppender::outputLog(const wchar_t * lpszContent, const LogLevel & logLevel, const wchar_t * lpszTimeStamp
	, const unsigned long & uThreadID)
{
	auto index = m_pLogList->InsertItem(m_pLogList->GetItemCount(), GetLevelName(logLevel));
	m_pLogList->SetItemText(index, 1, lpszTimeStamp);
	wchar_t wcThreadID[65] = { 0 };
	_ui64tow_s(uThreadID, wcThreadID, _countof(wcThreadID), 10);
	m_pLogList->SetItemText(index, 2, wcThreadID);
	m_pLogList->SetItemText(index, 3, lpszContent);
}
